package cn.blog.service.impl;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.blog.common.LayuiTableResult;
import cn.blog.common.PublicResultJosn;
import cn.blog.dao.PlayerUserMapper;
import cn.blog.entity.PlayerUser;
import cn.blog.entity.PlayerUserExample;
import cn.blog.entity.PlayerUserExample.Criteria;
import cn.blog.service.PlayerUserService;

@Service
public class PlayerUserServiceImpl implements PlayerUserService {

	@Autowired
	private PlayerUserMapper playerUserMapper;

	/**
	 * 添加一条选手信息
	 * 
	 * @param playerUser
	 * @return
	 */
	@Override
	@Transactional
	public PublicResultJosn add(PlayerUser playerUser) {
		// check pno
		if (this.checkPno(playerUser.getPno())) {
			throw new IllegalArgumentException("编号：" + playerUser.getPno() + "已存在");
		}
		playerUser.setPuid(UUID.randomUUID().toString());
		playerUser.setCreatetime(new Date());
		playerUserMapper.insert(playerUser);
		return new PublicResultJosn(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), playerUser);
	}

	/**
	 * 单、批量删除一条比赛信息
	 * 
	 * @param ids
	 * @return
	 */
	@Override
	@Transactional
	public PublicResultJosn delete(List<String> ids) {
		PlayerUserExample example = new PlayerUserExample();
		Criteria criteria = example.createCriteria();
		criteria.andPuidIn(ids);
		playerUserMapper.deleteByExample(example);
		return new PublicResultJosn(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), "操作成功");
	}

	/**
	 * 更新选手信息
	 * 
	 * @param playerUser
	 * @return
	 */
	@Override
	@Transactional
	public PublicResultJosn update(PlayerUser playerUser) {
		playerUserMapper.updateByPrimaryKeySelective(playerUser);
		return new PublicResultJosn(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), "操作成功");
	}

	/**
	 * 模糊分页查询选手信息
	 * 
	 * @param playerUser
	 * @param page
	 * @param pageSize
	 * @return
	 */
	@Override
	public LayuiTableResult query(PlayerUser playerUser, Integer page, Integer pageSize) {
		LayuiTableResult resultJosn = new LayuiTableResult();
		PlayerUserExample example = new PlayerUserExample();
		example.setOrderByClause("createtime DESC");
		Criteria criteria = example.createCriteria();
		// 构造条件
		if (StringUtils.isNoneBlank(playerUser.getPuid())) {
			// puid 查找
			criteria.andPuidLike("%" + playerUser.getPuid() + "%");
		} else {
			if (StringUtils.isNotBlank(playerUser.getName())) {
				criteria.andNameLike("%" + playerUser.getName() + "%");
			}
			if (StringUtils.isNoneBlank(playerUser.getPno())) {
				criteria.andPnoEqualTo(playerUser.getPno());
			}
		}
		// 开启分页查找
		PageHelper.startPage(page, pageSize);
		List<PlayerUser> list = playerUserMapper.selectByExample(example);
		PageInfo<PlayerUser> pageInfo = new PageInfo<>(list, pageSize);
		//
		resultJosn.setCode(HttpStatus.OK.value());
		resultJosn.setMsg(HttpStatus.OK.getReasonPhrase());
		resultJosn.setCount(pageInfo.getTotal());
		resultJosn.setData(pageInfo.getList());
		return resultJosn;
	}

	/**
	 * 根据id查询选手基本信息
	 * 
	 * @param id
	 * @return
	 */
	@Override
	public PublicResultJosn queryOne(String id) {
		PlayerUser playerUser = playerUserMapper.selectByPrimaryKey(id);
		return new PublicResultJosn(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), playerUser);
	}

	/**
	 * 检查pno，true：已经存在，false:不存在，可以使用此pno
	 * 
	 * @return
	 */
	private Boolean checkPno(String value) {
		PlayerUserExample example = new PlayerUserExample();
		Criteria criteria = example.createCriteria();
		criteria.andPnoEqualTo(value);
		List<PlayerUser> list = playerUserMapper.selectByExample(example);
		if (CollectionUtils.isEmpty(list)) {
			return false;
		}
		return true;
	}

}
